﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace AdvplDoc.View {

    abstract class AbstractPrinter {

        protected string outputPath;
        protected string fileExtension;

        public AbstractPrinter() {
        }

        public AbstractPrinter(string output) {
			if (!output.EndsWith("" + System.IO.Path.DirectorySeparatorChar))
				output += System.IO.Path.DirectorySeparatorChar;

			this.outputPath = output;

			if (!Directory.Exists(this.outputPath))
				Directory.CreateDirectory(this.outputPath);
		}

        public void write(string filename, StringBuilder content) {
            this.write(filename, content.ToString());
        }

        public void write(string filename, string content) {
            this.writeFile(this.outputPath, filename, content);
        }

        public void write(string directory, string filename, StringBuilder content) {
            this.write(directory, filename, content.ToString());
        }

        public void write(string directory, string filename, string content) {
            this.writeFile(this.outputPath + directory + "\\", filename, content);
        }

        private void writeFile(string directory, string filename, string content) {
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            StreamWriter fs = File.CreateText(directory + filename + "." + this.fileExtension);

            //fs.Encoding = Encoding.UTF8;
            fs.Write(content);
            fs.Flush();
            fs.Close();
        }

        internal abstract void process(Model.Parser parser, System.Action<int> start, System.Action<int> update);
    }
}
